package com.elong.qadev.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import com.elong.qadev.entity.CaseNode;
import com.elong.qadev.service.CaseServiceI;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 
* @ClassName: TreeOpAction
* @Description: TODO(前台tree节点操作相关)
* @author kangchen
* @date 2015年9月8日
*
 */
@ParentPackage("all")
public class TreeOpAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Logger logger = Logger.getLogger(TreeOpAction.class);

	@Autowired
	protected CaseServiceI caseService;
	/**
	 * 
	* @Title: addNode
	* @Description: TODO(添加用例的树节点)
	* @param @throws Exception    参数
	* @return void    返回类型
	* @throws
	 */
	@Action(value = "addNode")
	public void addNode() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setCharacterEncoding("UTF-8");
		String name = request.getParameter("name");
		String parentId = request.getParameter("parentId");
		String isParent = request.getParameter("isParent");
		String caseType = request.getParameter("caseType");
		logger.info("获取当前的父节点ID是：" + parentId);
		logger.info("获取当前的父节点NAME是：" + name);
		logger.info("获取当前节点是否父节点：" + isParent);
		logger.info("获取当前节点类型是：" + caseType);

		CaseNode cn = new CaseNode();
		cn.setName(name);
		cn.setCaseType(caseType);
		cn.setIsParent(isParent.equals("true") ? true : false);
		cn.setOpen(false);
		if (parentId != null) {
			cn.setpId(parentId);
		}
		cn.setRemarks("");
		cn = caseService.save(cn);
		logger.info("存入的节点数据为：" + cn.toString());

		String msg = cn.getId() + "";
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(msg);
		out.flush();
		out.close();
	}
	/**
	 * 
	* @Title: deleteNode
	* @Description: TODO(删除用例树节点)
	* @param     参数
	* @return void    返回类型
	* @throws
	 */
	@Action(value = "deleteNode")
	public void deleteNode() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");

		logger.info("删除的当前节点ID是：" + id);
		String msg = "";
		if ((id != null)) {
			String[] temp = id.split(",");
			for (int i = 0; i < temp.length; i++) {
				caseService.deleteNode(Integer.parseInt(temp[i]));
			}
			msg = "删除成功!";
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(msg);
		out.flush();
		out.close();
	}
	/**
	 * 
	* @Title: updateNode
	* @Description: TODO(编辑更新用例树节点)
	* @param @throws Exception    参数
	* @return void    返回类型
	* @throws
	 */
	@Action(value = "updateNode")
	public void updateNode() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setCharacterEncoding("UTF-8");
		String name = request.getParameter("name");
		String id = request.getParameter("id");
		logger.info("需要更新的当前节点ID是：" + id);
		logger.info("需要更新的当前节点name是：" + name);
		String msg = "";
		if (caseService.updateNode(Integer.parseInt(id), name)) {
			msg = "编辑成功!";
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(msg);
		out.flush();
		out.close();
	}
}
